package com.hqully.smartfumehood.controller;


import com.github.pagehelper.PageInfo;
import com.hqully.smartfumehood.common.utils.JWTUtil;
import com.hqully.smartfumehood.common.utils.PermissionUtil;
import com.hqully.smartfumehood.common.utils.ResultVoUtil;
import com.hqully.smartfumehood.common.vo.QueryVo;
import com.hqully.smartfumehood.common.vo.ResultVo;
import com.hqully.smartfumehood.entity.DevError;
import com.hqully.smartfumehood.entity.dto.DevErrorDto;
import com.hqully.smartfumehood.service.DevErrorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lly
 * @since 2022-03-10
 */
@RestController
@RequestMapping("/api/dev-error")
public class DevErrorController extends BaseController{
    @Autowired
    private DevErrorService devErrorService;

    @PostMapping("/find-all-dev-error")
    public ResultVo findAllDevError(@RequestHeader("token") String token,@RequestBody QueryVo queryVo){

        QueryVo realQueryVo = PermissionUtil.setQueryUserName(token, queryVo);

        System.out.println(JWTUtil.getUnitId(token));

        PageInfo<DevErrorDto> devErrorList = devErrorService.findAllDevError(realQueryVo);



        return ResultVoUtil.success(devErrorList);
    }
    
    @PostMapping("/add-dev-error")
    public ResultVo addDevError(@RequestBody DevError devError){

        try {
            int status = devErrorService.addDevError(devError);
        } catch (DuplicateKeyException e) {
            return ResultVoUtil.error("该设备号已存在");
        }


        return ResultVoUtil.success("添加异常成功");
    }

    @PostMapping("/delete-dev-error")
    public ResultVo deleteDevError(@RequestBody DevError DevError){
        devErrorService.deleteDevErrorById(DevError.getId());

        return ResultVoUtil.success("删除异常成功");
    }

    @PostMapping("/update-dev-error")
    public ResultVo updateDevError(@RequestBody DevError devError){
        devErrorService.updateDevError(devError);

        return ResultVoUtil.success("更新异常成功");
    }

    @GetMapping("/have-read")
    public ResultVo haveRead(@RequestHeader("token") String token,long devErrorId){

        String username = JWTUtil.getUsername(token);
        devErrorService.haveRead(devErrorId,username);

        return ResultVoUtil.success();
    }

    @GetMapping("/have-processed")
    public ResultVo haveProcessed(long devErrorId){

        devErrorService.haveProcessed(devErrorId);

        return ResultVoUtil.success();
    }

    @PostMapping("/find-unread-count")
    public ResultVo findUnreadCount(@RequestHeader("token") String token,@RequestBody QueryVo queryVo){

        QueryVo realQueryVo = PermissionUtil.setQueryUserName(token, queryVo);

        int unreadCount = devErrorService.findUnreadCount(realQueryVo);

        Map resultMap = new HashMap<String,Integer>();

        resultMap.put("unreadCount",unreadCount);

        return ResultVoUtil.success(resultMap);
    }

}
